package com.copyctrl.campus.portal.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.copyctrl.campus.common.constant.RedisConstant;
import com.copyctrl.campus.common.domain.User;
import com.copyctrl.campus.portal.dto.FoundDTO;
import com.copyctrl.campus.portal.service.FoundService;
import com.copyctrl.mapper.CampFoundMapper;
import com.copyctrl.model.CampFollowExample;
import com.copyctrl.model.CampFound;
import com.copyctrl.model.CampFoundExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author:JH
 * @version:1.0
 */
@Service
public class FoundServiceImpl implements FoundService {
    @Autowired
    CampFoundMapper mapper;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public int add(CampFound found) {
        found.setCreateTime(new Date());
        return mapper.insert(found);
    }

    @Override
    public int delete(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(CampFound found) {
        if (found.getDone()==1){
            found.setDoneTime(new Date());
        }
        CampFoundExample example = new CampFoundExample();
        example.createCriteria().andIdEqualTo(found.getId());
        return mapper.updateByExampleSelective(found, example);
    }

    @Override
    public Map findAll(int uid) {
        Map<String,List<CampFound>> map=new HashMap<>();
        CampFoundExample example = new CampFoundExample();
        example.createCriteria().andIdIsNotNull().andTypeEqualTo(0);
        example.setOrderByClause("`done`ASC,`create_time`DESC");
        map.put("found",mapper.selectByExample(example)
                .stream().map(found -> {
            FoundDTO foundDTO = BeanUtil.copyProperties(found, FoundDTO.class);
            String id = found.getUid().toString();
            Object openid = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, id);
            String s = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
            User user = JSONUtil.toBean(s, User.class);
            foundDTO.setUser(user);
            if (foundDTO.getGid()!=null){
                String gid = found.getGid().toString();
                Object openid1 = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, gid);
                String s1 = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid1);
                User user1 = JSONUtil.toBean(s1, User.class);
                foundDTO.setGUser(user1);
            }
            return foundDTO;
        }).collect(Collectors.toList()));
        example.clear();
        example.createCriteria().andIdIsNotNull().andTypeEqualTo(1);
        example.setOrderByClause("`done`ASC,`create_time`DESC");
        map.put("lost",mapper.selectByExample(example).stream().map(found -> {
            FoundDTO foundDTO = BeanUtil.copyProperties(found, FoundDTO.class);
            String id = found.getUid().toString();
            Object openid = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, id);
            String s = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
            User user = JSONUtil.toBean(s, User.class);
            foundDTO.setUser(user);
            if (foundDTO.getGid()!=null){
                String gid = found.getGid().toString();
                Object openid1 = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, gid);
                String s1 = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid1);
                User user1 = JSONUtil.toBean(s1, User.class);
                foundDTO.setGUser(user1);
            }
            return foundDTO;
        }).collect(Collectors.toList()));
        example.clear();
        example.createCriteria().andIdIsNotNull().andUidEqualTo(uid);
        example.setOrderByClause("`done`ASC,`create_time`DESC");
        map.put("mine",mapper.selectByExample(example).stream().map(found -> {
            FoundDTO foundDTO = BeanUtil.copyProperties(found, FoundDTO.class);
            String id = found.getUid().toString();
            Object openid = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, id);
            String s = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
            User user = JSONUtil.toBean(s, User.class);
            foundDTO.setUser(user);
            if (foundDTO.getGid()!=null){
                String gid = found.getGid().toString();
                Object openid1 = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, gid);
                String s1 = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid1);
                User user1 = JSONUtil.toBean(s1, User.class);
                foundDTO.setGUser(user1);
            }
            return foundDTO;
        }).collect(Collectors.toList()));
        return map;
    }

    @Override
    public List<FoundDTO> findByUid(Integer id) {
        CampFoundExample example = new CampFoundExample();
        example.createCriteria().andUidEqualTo(id);
        List<FoundDTO> campFounds = mapper.selectByExample(example).stream().map(found -> {
            FoundDTO foundDTO = BeanUtil.copyProperties(found, FoundDTO.class);
            Integer uid = found.getUid();
            Object openid = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, uid);
            String s = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
            User user = JSONUtil.toBean(s, User.class);
            foundDTO.setUser(user);
            if (foundDTO.getGid()!=null){
                Integer gid = found.getGid();
                Object openid1 = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, gid);
                String s1 = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid1);
                User user1 = JSONUtil.toBean(s, User.class);
                foundDTO.setGUser(user1);
            }
            return foundDTO;
        }).collect(Collectors.toList());
        return campFounds;
    }

    @Override
    public List<FoundDTO> findNotFound() {
        CampFoundExample example = new CampFoundExample();
        example.createCriteria().andIdIsNotNull().andGidIsNull();
        return mapper.selectByExample(example).stream().map(found -> {
            FoundDTO foundDTO = BeanUtil.copyProperties(found, FoundDTO.class);
            Integer uid = found.getUid();
            Object openid = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, uid);
            String s = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
            User user = JSONUtil.toBean(s, User.class);
            foundDTO.setUser(user);
            if (foundDTO.getGid()!=null){
                Integer gid = found.getGid();
                Object openid1 = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, gid);
                String s1 = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid1);
                User user1 = JSONUtil.toBean(s, User.class);
                foundDTO.setGUser(user1);
            }
            return foundDTO;
        }).collect(Collectors.toList());
    }
}
